﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.IO;

namespace SimplePad.Plugins
{
    public class PluginManager2
	{
        protected static PluginManager2 instance = null;

        protected PluginManager2()
        {
        }

        public static PluginManager2 getInstance()
        {
            if (instance == null)
            {
                instance = new PluginManager2();
            }
            return instance;
        }

		public static Hashtable FindPlugins(string sPath)
		{
			var plugins = new Hashtable();
			var rFiles = Directory.GetFiles(sPath, "*.dll");

			foreach (var sFile in rFiles)
			{
				try
				{
					var assembly = Assembly.LoadFile(sFile);

					foreach (var type in assembly.GetTypes())
					{
						var iface = type.GetInterface(typeof(IPlugin).ToString());

						if (iface != null)
						{
							IPlugin plugin = Activator.CreateInstance(type) as IPlugin;
                            plugins.Add(sFile, plugin);
						}
					}
				}
				catch (Exception ex)
				{
                    throw new Exception("Ошибка загрузки плагина\n" + ex.Message + "\n" + ex.StackTrace);
				}
			}
			return plugins;
		}
	}
}
